package com.example.demo.service.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: ak
 * @Date: 2020/4/12 20:59
 */
public class CommonUtils {
    public static String getFormatTime() {
        //格式化时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String formatTime(Long time){
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 传入时间戳，转换成定时任务字符串，定时任务将在结束时间前一秒完成
     *
     * @param timeStamp 结束时间的时间戳
     * @return 对应的定时任务cron
     */
    public static String getCronString(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(new Date()));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //提前一秒完成任务
        String sd = simpleDateFormat.format(new Date(timeStamp - 1000));
        String[] s = sd.split(" ");
        String[] timePrefix = s[0].split("-");
        String[] timeSuffix = s[1].split(":");
        return timeSuffix[2] + " " + timeSuffix[1] + " " + timeSuffix[0] + " " +
                timePrefix[2] + " " + timePrefix[1] + " ? *";
    }
}
